﻿
namespace Triad.DAL
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Configuration;
    using Triad.Common;
    using Triad.Model;

    /// <summary>
    /// General DB Stored Procs
    /// </summary>
    public class frameworkDBSProcs : DBBase
    {
        /// <summary>
        /// Dynamically creates OrderDB class (LINQ, Lambda or Sprocs) by reading from web.config file 'appSettings'
        /// </summary>
        /// <returns>new Instance of the Class</returns>
        public static frameworkDBSProcs Create()
        {
            string typeString = ConfigurationManager.AppSettings["DALType"];
            Type dbType = Type.GetType("Triad.DAL.framework" + typeString);
            return (frameworkDBSProcs)Activator.CreateInstance(dbType);
        }
        #region Select Statements

  
        /// <summary>
        /// Gets the user by user id.
        /// </summary>
        /// <param name="p_UserId">The p_ user id.</param>
        /// <returns></returns>
        public virtual WebUser GetUserByUserId(int p_UserId)
        {
            using (TriadDataContext context = DataContext)
            {
                return context.usp_WebUserById_Select(p_UserId).FirstOrDefault();
            }
        }

        /// <summary>
        /// Gets the user by employee number.
        /// </summary>
        /// <param name="p_UserName">The p_ employee number.</param>
        /// <returns></returns>
        public virtual WebUser GetUserByUserName(string p_UserName)
        {
            using (TriadDataContext context = DataContext)
            {
                return context.usp_WebUserByUserName_Select(p_UserName).FirstOrDefault();
            }
        }

        public virtual IEnumerable<usp_MenuItems_SelectResult> GetMenuItems(string p_CultureName, bool p_VisibleOnly,
            bool? p_ActiveOnly)
        {
            using (TriadDataContext context = DataContext)
            {

                return context.usp_MenuItems_Select(p_CultureName, p_VisibleOnly).ToArray();
            }
        }


        /// <summary>
        /// Gets all users. - LINQ Query
        /// </summary>
        /// <returns></returns>

        public WebUser[] GetAllUsers()
        {
            using (TriadDataContext context = DataContext)
            {
                WebUser[] user = (from c in context.WebUsers
                                  select c).ToArray();
                return user;
            }

        }

        #endregion
        #region Update Methods

        /// <summary>
        /// Updates the user.
        /// </summary>
        /// <param name="userObj">The user obj.</param>
        /// <returns></returns>
        public virtual OperationStatus UpdateUser(store.User userObj)
        {
            using (TriadDataContext context = DataContext)
            {
                try
                {
                    //context.usp_WebUser_Update(userObj.UserName, userObj.Sid, userObj.Culture, userObj.Theme, Convert.ToInt32(userObj.WebUserId),
                      //                         userObj.MachineID, ref str_dbErrMsg);
                }
                catch (Exception exp)
                {
                    return new OperationStatus(false, exp.Message, exp);
                }
                finally
                {
                    if (context != null) context.Dispose();
                }
            }
            return new OperationStatus(true, null, null);
        }

        public virtual OperationStatus UpdateMenuItem(int p_MenuItemId, string p_MenuLabel, int p_ReferenceIdMenuItemLabel, string p_Culture, bool p_Visible, short p_SortOrder, string p_TargetUrl)
        {
            using (TriadDataContext context = DataContext)
            {
                try
                {
                    context.usp_MenuItem_Update(p_MenuItemId, p_MenuLabel,p_ReferenceIdMenuItemLabel,p_Culture,
                                               p_Visible, p_SortOrder, p_TargetUrl);
                }
                catch (Exception exp)
                {
                    return new OperationStatus(false, exp.Message, exp);
                }
                finally
                {
                    if (context != null) context.Dispose();
                }
            }
            return new OperationStatus(true, null, null);
        }
        #endregion

        #region Delete Methods

        public virtual bool? DeleteMenuItem(int p_MenuItemId)
        {
            bool? retVal=false;
            using (TriadDataContext context = DataContext)
            {
                    context.usp_MenuItem_Delete(p_MenuItemId,ref retVal);
            }

            return retVal;

        }

        /// <summary>
        /// Disables the user.
        /// </summary>
        /// <param name="user">The user.</param>
        public static void DisableUser(WebUser user)
        {
        }

        #endregion

        #region Insert Methods
        /// <summary>
        /// Inserts the user_ USP.
        /// </summary>
        /// <param name="p_UserName">Name of the p_ user.</param>
        /// <param name="p_CultureName">Name of the p_ culture.</param>
        /// <param name="p_Theme">The p_ theme.</param>
        /// <param name="p_UserIdCreatedBy">The p_ user id created by.</param>
        /// <param name="p_StationCreatedAt">The p_ station created at.</param>
        /// <param name="p_UserIdModifiedBy">The p_ user id modified by.</param>
        /// <param name="p_StationModifiedAt">The p_ station modified at.</param>
        /// <param name="p_plxUserId">The p_ mes user id.</param>
        public virtual void InsertUser_USP(string p_UserName,  string p_CultureName, string p_Theme, int? p_UserIdCreatedBy, string p_StationCreatedAt, int? p_UserIdModifiedBy, string p_StationModifiedAt, ref int? p_plxUserId)
        {
            using (TriadDataContext context = DataContext)
            {
                context.usp_WebUser_Insert(p_UserName, p_CultureName, p_Theme, p_UserIdCreatedBy,
                                           p_StationCreatedAt, p_UserIdModifiedBy, p_StationModifiedAt, ref p_plxUserId);
            }
        }


        /// <summary>
        /// Inserts the user.
        /// </summary>
        /// <param name="user">The user.</param>
        public virtual void InsertUser_USP(WebUser user)
        {
            int? userID = null;

            InsertUser_USP(user.UserName, user.CultureName,  user.Theme, user.CreatedBy, user.StationCreatedAt, user.ModifiedBy,
                 user.StationModifiedAt, ref userID);
        }

        /// <summary>
        /// Inserts the user.
        /// </summary>
        /// <param name="user">The user.</param>
        /// <returns></returns>

        public virtual OperationStatus InsertUser_Direct(WebUser user)
        {
            using (TriadDataContext context = DataContext)
            {
                try
                {
                    context.WebUsers.InsertOnSubmit(user);
                    context.SubmitChanges();
                }
                catch (Exception exp)
                {
                    return new OperationStatus(false, exp.Message, exp);
                }
                finally
                {
                    if (context != null) context.Dispose();
                }
            }
            return new OperationStatus(true, null, null);
        }


        public virtual OperationStatus InsertMenuItem_USP(string p_MenuLabel, int p_MenuItemIdParent, bool p_Visible, short p_SortOrder, string p_TargetUrl)
        {
            using (TriadDataContext context = DataContext)
            {
                context.usp_MenuItem_Insert(p_MenuLabel, p_MenuItemIdParent, p_Visible, p_SortOrder, p_TargetUrl);
            }
            
            return new OperationStatus(true, null, null);
        }
        #endregion

    }
}
